//
//  ZYHistoricalDataVC.m
//  YiLin
//
//  Created by Zyark on 2018/6/11.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYHistoricalDataVC.h"
#import "UICurveView.h"
#import "ZYColorCheckButton.h"
#import "ZYCurve.h"
#import "UIColor+HTML.h"
#import "EBDropdownListView.h"

@interface ZYHistoricalDataVC ()<UICurveViewDelegate>

/**
 按钮组背景
 */
@property(nonatomic,strong) UIView *buttonBgView;

/**
 按钮组
 */
@property(nonatomic,strong) NSMutableArray *buttonArray;

/**
 曲线View
 */
@property(nonatomic,strong) UICurveView *curveView;

/**
 系统曲线
 */
@property(nonatomic,strong) NSArray *curveModels;


/**
 当前曲线类型
 */
@property(nonatomic,assign) ZYCurveType currentType;


/**
 下拉选择
 */
@property(nonatomic,strong) EBDropdownListView *listView;

/**
 选中底部线
 */
@property(nonatomic,strong) UIView *selectLine;
@end

@implementation ZYHistoricalDataVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_HOME_EDIT_DEVICE_HISTORICAL_DATA];
}

-(void)setup{
    [self setupView];
    [self setupData];
}

-(void)setupView{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    
    _buttonBgView = [[UIView alloc] init];
    _buttonBgView.size = CGSizeMake(ZYSCREEN_W - 30, 47);
    _buttonBgView.backgroundColor = RGBA(248, 248, 248, 1);
    _buttonBgView.layer.cornerRadius = 5;
    _buttonBgView.layer.masksToBounds = YES;
    [self.view addSubview:_buttonBgView];
    _buttonBgView.centerX = self.view.centerX;
    _buttonBgView.top = self.navigationView.bottom;
    
    NSArray *titleArray = @[[language getStringByKey:LC_INDEX_MAIN_ROOM_OUT_LABEL],
                            [language getStringByKey:LC_INDEX_MAIN_BEDROOM_LABEL],
                            [language getStringByKey:LC_INDEX_MAIN_CHILDREN_ROOM_LABEL],
                            [language getStringByKey:LC_INDEX_MAIN_LIVING_ROOM_LABEL]];
    NSArray *curveTypeArray = @[[NSNumber numberWithUnsignedInteger:ZYHomeRoomTypeOut],
                                [NSNumber numberWithUnsignedInteger:ZYHomeRoomTypeBedroom],
                                [NSNumber numberWithUnsignedInteger:ZYHomeRoomTypeChildrenRoom],
                                [NSNumber numberWithUnsignedInteger:ZYHomeRoomTypeLivingRoom]];
    NSArray *colorStringArray = @[@"#faa4c9",@"#a8deff",@"#ebcdf3",@"#ceea95"];
    CGFloat btnWidth = _buttonBgView.width / 4;
    _buttonArray = [NSMutableArray arrayWithCapacity:4];
    for (NSInteger i=0; i<4; i++) {
        ZYColorCheckButton *button = [[ZYColorCheckButton alloc] initWithFrame:CGRectMake(i * btnWidth, 0, btnWidth, _buttonBgView.height)];
        button.title = [titleArray objectAtIndex:i];
        button.checkBackColor = [UIColor ColorFromHtml:[colorStringArray objectAtIndex:i]];
        button.textColor = [UIColor ColorFromHtml:@"#9d9d9d"];
        button.font = [UIFont systemFontOfSize:12];
        button.checkImage = [UIImage imageNamed:@"ic_policy_curve_btn_select"];
        button.tag = [[curveTypeArray objectAtIndex:i] integerValue];
        [button addTarget:self action:@selector(selectSysBtn:) forControlEvents:UIControlEventTouchUpInside];
        button.checked = NO;
        [_buttonBgView addSubview:button];
        [_buttonArray addObject:button];
    }
    
    //底部线条
    _selectLine = [[UIView alloc] initWithFrame:CGRectMake(0, _buttonBgView.height - 1, btnWidth - 1, 1)];
    _selectLine.backgroundColor = [UIColor ColorFromHtml:@"#FA967F"];
    [_buttonBgView addSubview:_selectLine];
    
    _curveView = [[UICurveView alloc] initWithFrame:CGRectMake(0, 0, ZYSCREEN_W - ZYSCREEN_W * 0.16, 170)];
    _curveView.xChartBlock = ChartBlockMake(0, 3, 40, 8);
    _curveView.yChartBlock = ChartBlockMake(0, 5, 20, 6);
    _curveView.delegate = self;
    [self.view addSubview:_curveView];
    _curveView.centerX = self.view.centerX;
    _curveView.top = _buttonBgView.bottom + 8;
    [self setupListView];
}

//切换曲线
-(void)selectSysBtn:(id)sender
{
    ZYColorCheckButton *btn = (ZYColorCheckButton *)sender;
    if(btn.tag == _currentType){
        return;
    }
    [self doSelectSysBtn:btn];
}

-(void)doSelectSysBtn:(ZYColorCheckButton *)btn{
    NSLog(@"btn.tag:%ld",btn.tag);
    _currentType = btn.tag;
    
    NSInteger index = [_buttonArray indexOfObject:btn];
    CGFloat btnWidth = _buttonBgView.width / 4;
    CGRect frame = _selectLine.frame;
    frame.origin.x = index * btnWidth;
    
    [UIView animateWithDuration:0.3f animations:^{
        _selectLine.frame = frame;
    }];
    //重新排序曲线层次，并重新绘制
    NSMutableArray *_models = [NSMutableArray arrayWithArray:_curveModels];
    CurveModel *_tempModel;
    for (CurveModel *model in _curveModels) {
        if (model.type == btn.tag) {
            _tempModel = model;
            [_models removeObject:model];
        }
    }
    if(_tempModel){
        //将当前的曲线排在最前面
        [_models insertObject:_tempModel atIndex:0];
        _curveModels = [[_models reverseObjectEnumerator] allObjects];
        _curveView.curveItems = _curveModels;
    }
}

-(void)setupListView{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    NSArray *array = @[[language getStringByKey:LC_HISTORICAL_DATA_LABEL_DAY],
                       [language getStringByKey:LC_HISTORICAL_DATA_LABEL_WEEK],
                       [language getStringByKey:LC_HISTORICAL_DATA_LABEL_MONTH],
                       [language getStringByKey:LC_HISTORICAL_DATA_LABEL_YEAR]];
    NSInteger count = 0;
    NSMutableArray *dataSource = [NSMutableArray arrayWithCapacity:[array count]];
    for (NSString *title in array) {
        EBDropdownListItem *item = [[EBDropdownListItem alloc] initWithItem:[NSString stringWithFormat:@"%ld",count] itemName:title];
        [dataSource addObject:item];
    }
    _listView = [[EBDropdownListView alloc] initWithDataSource:dataSource];
    _listView.size = CGSizeMake(130, 35);
    [self.view addSubview:_listView];
    _listView.centerX = self.view.centerX;
    _listView.top = _curveView.bottom + 10;
    _listView.backgroundColor = RGBA(248, 248, 248, 1);
    _listView.layer.cornerRadius = 5;
    _listView.layer.masksToBounds = YES;
    _listView.font = [UIFont systemFontOfSize:13];
    _listView.textColor = RGBA(153, 153, 153, 1);
    _listView.selectTextColor = RGBA(51, 51, 51, 1);
    [_listView setDropdownListViewSelectedBlock:^(EBDropdownListView *dropdownListView) {
        
    }];
}

-(void)setupData{
//    _curveModels = nil;
//    _curveModels = [NSArray array];
//    NSBundle *bundle = [NSBundle mainBundle];
//    NSString *file = [bundle pathForResource:@"curve_list" ofType:@"plist"];
//    NSArray *array = [NSArray arrayWithContentsOfFile:file];
//    ZYCurve *curveInfo;
//    CurveModel *curveitem;
//    NSMutableArray *models = [[NSMutableArray alloc] init];
//    for (NSDictionary *info in array) {
//        curveInfo = [[ZYCurve alloc] initWithDictionary:info];
//        curveitem = [[CurveModel alloc] init];
//        curveitem.name = curveInfo.name;
//        curveitem.curveID = curveInfo.curveID;
//        curveitem.type = curveInfo.type;
//        switch (curveitem.type) {
//            case ZYCurveTypeAutomaticLearning:
//                curveitem.fillColor = [[UIColor ColorFromHtml:@"#faa4c9"] colorWithAlphaComponent:0.5];
//                curveitem.lineColor = [UIColor ColorFromHtml:@"#faa4c9"];
//                break;
//            case ZYCurveTypeElderlyChildren:
//                curveitem.fillColor = [[UIColor ColorFromHtml:@"#a8deff"] colorWithAlphaComponent:0.5];
//                curveitem.lineColor = [UIColor ColorFromHtml:@"#a8deff"];
//                break;
//            case ZYCurveTypeSouth:
//                curveitem.fillColor = [[UIColor ColorFromHtml:@"#ebcdf3"] colorWithAlphaComponent:0.5];
//                curveitem.lineColor = [UIColor ColorFromHtml:@"#ebcdf3"];
//                break;
//            case ZYCurveTypeNorth:
//                curveitem.fillColor = [[UIColor ColorFromHtml:@"#ceea95"] colorWithAlphaComponent:0.5];
//                curveitem.lineColor = [UIColor ColorFromHtml:@"#ceea95"];
//                break;
//
//            default:
//                break;
//        }
//        NSMutableArray *hourTempures = [[NSMutableArray alloc] init];
//        for (NSDictionary *dic in [info objectForKey:@"tempHours"]) {
//            NSInteger hour = [NSString stringWithFormat:@"%@",[dic objectForKey:@"hour"]].integerValue;
//            //取3的整除小时
//            //            if (hour % 3 == 0) {
//            hour = hour / 3;
//            CGFloat tempure = [NSString stringWithFormat:@"%@",[dic objectForKey:@"temp"]].floatValue * 5;
//            [hourTempures addObject:[NSValue valueWithHourTempure:HourTempureValueMake(hour, tempure)]];
//            //            }
//        }
//
//        curveitem.hourtempures = hourTempures;
//        [models addObject:curveitem];
//    }
//    _curveModels = models;
//    _curveView.curveItems = _curveModels;
}

#pragma mark -UICurveViewDelegate
//长按选中曲线
-(void)curveView:(UICurveView *)sender longPressed:(CurveModel *)model{
    NSLog(@"longpressed");
}

@end
